package com.classes;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import com.classes.accessflags.AccessFlag;
import com.classes.attributes.AttributeContext;
import com.classes.attributes.logic.Attribute;
import com.classes.classreference.ClassReference;
import com.classes.classreference.InterfaceReference;
import com.classes.classreference.SuperClassReference;
import com.classes.constants.ConstantsContext;
import com.classes.constants.logic.Constants;
import com.classes.field.FiledTable;
import com.classes.method.MethodTable;

public class Class {

    //常量池大小
    public static short contants_pool_size;


    public static void main(String[] args) throws IOException {
        FileChannel channel = FileChannel.open(Paths.get("D:/Test.class"), StandardOpenOption.READ);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int read = channel.read(buffer);

        if (read > 0) {
            buffer.flip();

            //魔数 CAFEBABE
            System.out.println(Magic.magic(buffer));

            //版本
            System.out.println(Version.version(buffer));

            //常量池
            contants_pool_size = Constants.constants_size(buffer);
            System.out.println("常量池大小（十进制）：" + contants_pool_size + " 索引范围1-" + contants_pool_size);
            System.out.println("常量池内容如下：");
            for (int i = 0; i < contants_pool_size; i++) {
                String tag = Integer.toUnsignedString(buffer.get());
                ConstantsContext context = new ConstantsContext(Integer.parseInt(tag));
                context.parse(buffer);
                Constants.contants_pool.add(context.getConstant());
            }
            Constants.contants_pool.stream().forEach(constant -> {
                System.out.println(constant.toString());
            });


            //访问标识
            AccessFlag.accessFlag(buffer);

            //类引用
            ClassReference.reference(buffer);

            //父类引用
            SuperClassReference.reference(buffer);

            //接口引用
            InterfaceReference.reference(buffer);

            //字段集合
            FiledTable.fieldInfo(buffer);

            //方法集合
            MethodTable.methodInfo(buffer);

            //属性表
            short attributesLength = Attribute.attributesLength(buffer);
            System.out.println("属性表大小：" + attributesLength);

            for (int i = 0; i < attributesLength; i++) {
                AttributeContext context = new AttributeContext(buffer);
                context.parse();
            }


        }
        channel.close();
    }

}
